import systemSerive from "@/serive/systemSerive";
export default ({
    state: {
        departmentData: [], //部门数据列表
        UserList: [], // 用户数据列表
        total: 0, // 用户总条数
        roleTotal: 0, // 角色总条数
        userUpdateList: [], // 修改信息的回显数据
        roleList: [], // 角色数据列表
        ListPerms: [], // 全部权限列表
        CurrentRoleList: [], // 角色拥有权限id 集合
    },
    actions: {
        async showDepartment({ commit }) {
            let result = await systemSerive.showDepartment();
            // console.log(result.data);
            let res = result.data.data
            this.departmentData = result.data
            commit("setDepartmentData", res)
        },
        async departmentAdd({ commit }, { departmentName, departmentNumber, fid }) {
            let result = await systemSerive.departmentAdd(departmentName, departmentNumber, fid);
            return result.data
        },
        async delDepartmentById({ commit }, id) {
            console.log("in");
            let result = await systemSerive.delDepartmentById(id);
            return result.data;
        },
        // 查询角色信息
        async queryUserAll({ commit }, { limit, name, page }) {
            let result = await systemSerive.queryUserAll(limit, name, page)
            commit("setRoleList", result.data.data)
            commit("setTotal", result.data.count)
        },
        async userAdd({ commit }, { name, password, username }) {
            console.log({ name, password, username });
            let result = await systemSerive.userAdd(name, password, username)
            return result.data
        },
        // 修改用户信息
        async userUpdate({ commit }, { name, password, username, id }) {
            console.log("userUpdate", { name, password, username, id });
            let result = await systemSerive.userUpdate(id, name, password, username)
            return result.data
        },
        // 返回修改信息
        async userById({ commit }, id) {
            let result = await systemSerive.userById(id)
            return result.data.data
        },
        //修改部门
        async updateDepartment({ commit }, { did, uid }) {
            let result = await systemSerive.updateDepartment(did, uid)
            return result.data
        },
        // 返回角色信息
        async roleAll({ commit }, { limit, name, page }) {
            let result = await systemSerive.roleAll(limit, name, page)
            commit("setRole", result.data)
        },
        // 配置用户拥有角色
        async userRole({ commit }, { rid, uid }) {
            let result = await systemSerive.userRole(rid, uid)
            return result.data
        },
        // 修改角色信息
        async roleUpdate({ commit }, { id, roleDesc, roleName }) {
            let result = await systemSerive.roleUpdate(id, roleDesc, roleName)
            return result.data
        },
        // 获取全部权限列表
        async queryListPerms({ commit }) {
            console.log("in");
            let res = await systemSerive.queryListPerms()
            commit("setListPerms", res.data)

        },
        // 获取角色已经拥有的权限
        async queryCurrentRole({ commit }, id) {
            let res = await systemSerive.queryCurrentRole(id)
            console.log(res.data);
            commit("setCurrentRoleList", res.data)
            return res.data
        },
        // 修改角色拥有权限
        async permUpdate({ commit }, { ids, rid }) {
            let res = await systemSerive.permUpdate(ids, rid)
            console.log(res.data);
            return res.data
        },
        // 新增角色
        async addRole({ commit }, { roleDesc, roleName }) {
            let res = await systemSerive.addRole(roleDesc, roleName)
            // console.log("res.data", res.data);
            return res.data
        },
        // 删除角色
        async deleteRole({commit},id){
            let res = await systemSerive.deleteRole(id)
            console.log(res.data);
            return res.data
        },
        async UserDelete({commit},id){
            let res = await systemSerive.UserDelete(id)
            console.log("删除",res.data);
            return res.data
        }
    },
    mutations: {
        setDepartmentData(state, data) { //设置部门信息
            let res = data
            state.departmentData = res;
        },
        setRoleList(state, data) {//获取用户信息
            state.UserList = data
        },
        // 返回总条数
        setTotal(state, data) {
            state.total = data
        },
        // 设置修改返回的数据
        setuserUpdataList(state, data) {
            console.log(data);
            state.userUpdateList = data.data
        },
        setRole(state, data) {//设置角色相关数据
            state.roleList = data.data // 设置角色数组
            state.roleTotal = data.count // 设置总条数
        },
        setListPerms(state, data) {
            state.ListPerms = data.data
        },
        setCurrentRoleList(state, data) {
            state.CurrentRoleList = data.data
        }
    },
    getters: {
        getDepartmentData(state) { //获取部门
            return [...state.departmentData]
        },
        getRoleList(state) { // 获取用户列表
            return [...state.UserList]
        },
        getTatol(state) { // 获取用户总页数
            return state.total
        },
        // 获取用户修改回显信息
        getUserUpdateList(state) {
            return [...state.userUpdateList]
        },
        getAllRole(state) { // 获取角色信息列表
            console.log("in", state.roleList);
            return [...state.roleList]
        },
        getroleTotal(state) { // 获取角色总页数
            return state.roleTotal
        },
        getListPerms(state) {// 获取全部权限列表
            if (state.ListPerms==null) {
                console.log("inneris");
                return []
            }
            console.log("List",state.ListPerms);
            return [...state.ListPerms]
        },
        getCurrentRoleList(state) {
            return [...state.CurrentRoleList]
        }

    }


})